﻿using MS_Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MS_UI
{
    public partial class FromMain : Form
    {
        public FromMain()
        {
            InitializeComponent();  
            this.Load += FromMain_Load;
        }
        // 定义根目录路径
        private string FilePath = Application.StartupPath + "\\ConfigFile\\";
        // 实例化通讯线程
        CommThread objComm = new CommThread();
        // 实例化插入数据
        InsertData objInsert = new InsertData(1000);

        private void FromMain_Load(object sender, EventArgs e)
        {
            // 初始化文件信息
            InitialTxt();
            // 打开串口
            if (OpenPort(CommonMethod.objModbusEntity))
            {
                CommonMethod.CommOK = true;
                objComm.t.Start();
            }
            else
            {
                CommonMethod.CommOK = false;
            }
            this.btn_View_Click(null,null);
        }

        // 打开串口
        private bool OpenPort(ModbusEntity objModbus)
        {
           return CommonMethod.objMod.OpenMyComm(objModbus.Paud,objModbus.Port,objModbus.DataBit,objModbus.IParity,objModbus.IStopbit);
        }

        private void InitialTxt()
        {
            // 第一步：通讯接口信息
            CommonMethod.objModbusEntity = CommonMethod.GetConfigInfo(FilePath+"Modbus.ini");
            // 第二步：变量集合信息
            CommonMethod.VarModbusList = CommonMethod.LoadXML(FilePath + "Variable_Modbus.xml");
            // 第三步：存储区域集合
            CommonMethod.StoreAreaList = CommonMethod.LoadStoreAreaXML(FilePath + "StoreArea.xml");
            // 第四步：报警变量集合
            CommonMethod.VarAlarmModbusList = CommonMethod.LoadAlarmXml(FilePath + "VarAlarm_Modbus.xml");
            // 第五步：其他初始化
            foreach (Variable_Modbus item in CommonMethod.VarModbusList)
            {
                if (CommonMethod.CurrentValue.ContainsKey(item.VarName))
                {
                    CommonMethod.CurrentValue.Add(item.VarName,"");
                }
                if (item.StoreArea == "01 Coil Status(0x)")
                {
                    objComm.List_0x.Add(item);
                }

                if (item.StoreArea == "02 Input Status(1x)")
                {
                    objComm.List_1x.Add(item);
                }
                if (item.StoreArea == "03 Holding Register(4x)")
                {
                    objComm.List_4x.Add(item);
                }
                if (item.StoreArea == "04 Input Register(3x)")
                {
                    objComm.List_3x.Add(item);
                }

                if (item.IsFilling == "1")
                {
                    CommonMethod.FileVarModbusList.Add(item);
                    CommonMethod.CurrentVarNote.Add(item.VarName,item.Note);
                    CommonMethod.FileVarNameList.Add(item.Note);
                }
                if (item.IsReport == "1")
                {
                    CommonMethod.ReportVarModbusList.Add(item);
                }
            }
        }

        private void OpenWindow(Form Frm)
        {
            Frm.TopLevel = false;
            Frm.FormBorderStyle = FormBorderStyle.None;
            Frm.Dock = DockStyle.Fill;
            Frm.Parent = this.MainPanel;
            Frm.Show();
        }

        private bool CloseWindow(string FrmName)
        {
            foreach (Control ct in this.MainPanel.Controls)
            {
                if (ct is Form)
                {
                    Form frm = (Form)ct;
                    if (frm.Name == FrmName)
                    {
                        return true;
                    }
                    else
                    {
                        frm.Close();
                    }
                }
            }
            return false;
        }


        private void btn_View_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmView") == false)
            {
                OpenWindow(new FrmView());
                this.lbl_Title.Text = sender==null?"控制流程":((Button)sender).Text;
                BackColorSet(sender == null ? "控制流程" : ((Button)sender).Text);
            }
        }

        private void btn_ParamSet_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmParamSet") == false)
            {
                OpenWindow(new FrmParamSet());
                this.lbl_Title.Text = ((Button)sender).Text;
                BackColorSet(((Button)sender).Text);
            }

        }

        private void btn_Trend_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmThred") == false)
            {
                OpenWindow(new FrmTrend());
                this.lbl_Title.Text = ((Button)sender).Text;
                BackColorSet(((Button)sender).Text);
            }
        }

        private void btn_Alarm_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmAlarm") == false)
            {
                OpenWindow(new FrmAlarm());
                this.lbl_Title.Text = ((Button)sender).Text;
                BackColorSet(((Button)sender).Text);
            }
        }

        private void btn_Report_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmReport") == false)
            {
                OpenWindow(new FrmReport());
                this.lbl_Title.Text = ((Button)sender).Text;
                BackColorSet(((Button)sender).Text);
            }
        }

        private void btn_HardwareConfig_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmHardwareConfig") == false)
            {
                OpenWindow(new FrmHardwareConfig());
                this.lbl_Title.Text = ((Button)sender).Text;
                BackColorSet(((Button)sender).Text);
            }
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void BackColorSet(string item)
        {
            this.btn_View.BackColor = Color.FromArgb(11,115,163);
            this.btn_ParamSet.BackColor = Color.FromArgb(11,115,163);
            this.btn_Trend.BackColor = Color.FromArgb(11,115,163);
            this.btn_Alarm.BackColor = Color.FromArgb(11,115,163);
            this.btn_Report.BackColor = Color.FromArgb(11,115,163);
            this.btn_HardwareConfig.BackColor = Color.FromArgb(11,115,163);
            switch (item)
            {
                case "控制流程":
                    this.btn_View.BackColor = Color.FromArgb(60,179,113);
                    break;
                case "参数设置":
                    this.btn_ParamSet.BackColor = Color.FromArgb(60, 179, 113);
                    break;
                case "趋势曲线":
                    this.btn_Trend.BackColor = Color.FromArgb(60, 179, 113);
                    break;
                case "故障报警":
                    this.btn_Alarm.BackColor = Color.FromArgb(60, 179, 113);
                    break;
                case "数据报表":
                    this.btn_Report.BackColor = Color.FromArgb(60, 179, 113);
                    break;
                case "硬件组态":
                    this.btn_HardwareConfig.BackColor = Color.FromArgb(60, 179, 113);
                    break;
                default:
                    this.btn_View.BackColor = Color.FromArgb(60, 179, 113);
                    break;
            }



        }



    }
}
